package com.fenda.jetpacknavigation.roomdata.nav;

import android.content.Context;
import android.os.Bundle;

import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentActivity;
import androidx.lifecycle.ViewModelProvider;
import androidx.navigation.NavAction;
import androidx.navigation.Navigation;

import android.text.Editable;
import android.text.TextWatcher;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.inputmethod.InputMethodManager;
import android.widget.Button;
import android.widget.EditText;

import com.fenda.jetpacknavigation.R;
import com.fenda.jetpacknavigation.roomdata.MyUserViewModel;
import com.fenda.jetpacknavigation.roomdata.User;

import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.disposables.CompositeDisposable;
import io.reactivex.schedulers.Schedulers;

/**
 * A simple {@link Fragment} subclass.
 *
 */
public class AddFragment extends Fragment {
    private EditText edtFirstName,edtLastName;
    private Button button;
    private MyUserViewModel myViewModel;
    private InputMethodManager imm;

    public AddFragment() {

    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        return inflater.inflate(R.layout.fragment_add, container, false);
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        FragmentActivity activity = requireActivity();
        edtFirstName = activity.findViewById(R.id.edtFirstName);
        edtLastName = activity.findViewById(R.id.edtLastName);
        button = activity.findViewById(R.id.submit);
        button.setEnabled(false);

        myViewModel = new ViewModelProvider(activity,new ViewModelProvider.AndroidViewModelFactory(activity.getApplication())).get(MyUserViewModel.class);

        //自动弹出键盘
        edtFirstName.requestFocus();
        imm = (InputMethodManager) activity.getSystemService(Context.INPUT_METHOD_SERVICE);
        imm.showSoftInput(edtFirstName,0);

        //监听输入框的输入
        TextWatcher textWatcher = new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                String first = edtFirstName.getText().toString().trim();
                String last =  edtLastName.getText().toString().trim();
                button.setEnabled(!first.isEmpty() && !last.isEmpty());
            }

            @Override
            public void afterTextChanged(Editable s) {

            }
        };
        edtFirstName.addTextChangedListener(textWatcher);
        edtLastName.addTextChangedListener(textWatcher);
        final CompositeDisposable compositeDisposable = new CompositeDisposable();
        button.setOnClickListener(v ->  {
            button.setEnabled(false);
            String first = edtFirstName.getText().toString().trim();
            String last =  edtLastName.getText().toString().trim();
            User user = new User(first,last);
            compositeDisposable.add(
                    myViewModel.insert(user)
                    .subscribeOn(Schedulers.io())
                    .observeOn(AndroidSchedulers.mainThread())
                    .subscribe(() -> {
                        button.setEnabled(true);
                        Navigation.findNavController(getView()).navigateUp();
                        //关闭键盘
                        imm.hideSoftInputFromWindow(v.getWindowToken(),0);
                    })
            );
        });
    }

    @Override
    public void onResume() {
        imm.hideSoftInputFromWindow(requireView().getWindowToken(),0);
        super.onResume();
    }
}